package com.ojoin.trade.t0loan_core.web.dao.impl;

import java.util.HashMap;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import com.ojoin.trade.common.constdef.IConstDef;
import com.ojoin.trade.common.exception.BusinessException;
import com.ojoin.trade.common.exception.DaoException;
import com.ojoin.trade.common.web.dao.impl.BaseDaoImpl;
import com.ojoin.trade.t0loan_core.web.dao.TbT0LoanLiveApplicationDao;
import com.ojoin.trade.t0loan_core.web.domain.T0LoanLiveUserHistoryTransTotalInfoVO;
import com.ojoin.trade.t0loan_core.web.domain.TbT0LoanLiveApplication;
import com.ojoin.trade.t0loan_core.web.domain.TbT0LoanLiveInfoVO;
import com.ojoin.trade.t0loan_core.web.domain.TbT0LoanTodoTaskVO;

@Repository
public class TbT0LoanLiveApplicationDaoImpl extends BaseDaoImpl<TbT0LoanLiveApplication> 
	implements TbT0LoanLiveApplicationDao {
	
	public SqlSession getSqlSession() {
		return this.sqlSessionTemplate;
	}

	@Override
	public List<TbT0LoanLiveApplication> queryT0LoanLiveInfoByJoinUserID(
			int joinuserid) throws BusinessException {
		String mapMethodName = "queryT0LoanLiveInfoByJoinUserID";
		try {
			return getSqlSession().selectList(getSqlName(mapMethodName),
					joinuserid);
		} catch (Exception e) {
			throw new DaoException(String.format("根据joinuserid查询对象出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}

	@Override
	public List<TbT0LoanLiveApplication> queryT1LoanLiveInfoByJoinUserID(
			int joinuserid) throws BusinessException {
		String mapMethodName = "queryT1LoanLiveInfoByJoinUserID";
		try {
			return getSqlSession().selectList(getSqlName(mapMethodName),
					joinuserid);
		} catch (Exception e) {
			throw new DaoException(String.format("根据joinuserid查询对象出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}
	
	@Override
	public List<TbT0LoanLiveInfoVO> queryAllT0LoanLiveInfoByStatus(
			Integer loanstatus, Pageable page) throws BusinessException {
		String mapMethodName = "queryAllT0LoanLiveInfoByStatus";
		try {
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("loanstatus", loanstatus);
			return getSqlSession().selectList(getSqlName(mapMethodName),
					paramMap, this.getPageBounds(page));
		} catch (Exception e) {
			throw new DaoException(String.format("根据loanstatus->" + loanstatus + "查询对象出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}

	@Override
	public List<TbT0LoanLiveInfoVO> queryAllTxLoanLiveInfoByApplying(
			Pageable page, String operatetype) throws BusinessException {
		String mapMethodName = "queryAllTxLoanLiveInfoByApplying";
		try {
			return getSqlSession().selectList(getSqlName(mapMethodName), operatetype, 
					this.getPageBounds(page));
		} catch (Exception e) {
			throw new DaoException(String.format("查询对象出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}
	
	private Pageable getDefaultPage() {
		return new PageRequest(0, 5);
	}
	
	@Override
	public int updateT0LoanLiveInfoByLiveIDsAndStatus(int[] liveids, 
			int loanstatus) throws BusinessException {
		String mapMethodName = "updateT0LoanLiveInfoByLiveIDsAndStatus";
		try {
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("liveids", liveids);
			paramMap.put("loanstatus", loanstatus);
			
			return getSqlSession().update(getSqlName(mapMethodName), paramMap);
		} catch (Exception e) {
			throw new DaoException(String.format("根据joinuserid和loanstatus更新对象出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}

	@Override
	public int updateTxLoanLiveInfoByLiveIDsAndStatus(String operatetype, int[] liveids, int idOfTbMerchantUserOfCreditor, 
			int loanstatus) throws BusinessException {
		String mapMethodName = "updateTxLoanLiveInfoByLiveIDsAndStatus";
		try {
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("liveids", liveids);
			paramMap.put("creditor", idOfTbMerchantUserOfCreditor);
			paramMap.put("loanstatus", loanstatus);
			paramMap.put("operatetype", operatetype);
			
			return getSqlSession().update(getSqlName(mapMethodName), paramMap);
		} catch (Exception e) {
			throw new DaoException(String.format("根据joinuserid和loanstatus更新对象出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}
	@Override
	public TbT0LoanLiveApplication queryNewlyT0LoanLiveInfoByJoinUserID(
			int joinuserid) throws BusinessException {
		String mapMethodName = "queryNewlyT0LoanLiveInfoByJoinUserID";
		try {
			List list = getSqlSession().selectList(getSqlName(mapMethodName),
					joinuserid);
			if (list != null && list.size() >0)
				return (TbT0LoanLiveApplication) list.get(0);
		} catch (Exception e) {
			throw new DaoException(String.format("根据joinuserid查询最近一次T+0贷款申请记录出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
		return null;
	}

	public TbT0LoanLiveApplication queryNewlyTxLoanLiveInfoByJoinUserID(
			int joinuserid, String operatetype) throws BusinessException {
		String mapMethodName = "queryNewlyTxLoanLiveInfoByJoinUserID";
		try {
			HashMap<String, Object> param = new HashMap<String, Object>();
			param.put("joinuserid", joinuserid);
			param.put("operatetype", operatetype);
			
			List list = getSqlSession().selectList(getSqlName(mapMethodName),
					param);
			if (list != null && list.size() >0)
				return (TbT0LoanLiveApplication) list.get(0);
		} catch (Exception e) {
			throw new DaoException(String.format("根据joinuserid查询最近一次T+1贷款申请记录出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
		return null;
	}
	
	@Override
	public List<T0LoanLiveUserHistoryTransTotalInfoVO> queryT0LoanLiveUserHistoryTransTotalInfoList(Pageable page)
			throws BusinessException {
		String mapMethodName = "queryT0LoanLiveUserHistoryTransTotalInfoList";
		try {
			return getSqlSession().selectList(getSqlName(mapMethodName), this.getPageBounds(page));
		} catch (Exception e) {
			throw new DaoException(String.format("查询申请T+0贷款审核通过的九盈用户的当日前30日历史交易金额合计列表出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}

	@Override
	public int updateT0LoanLiveInfoFeeAndRatioByLiveID(int liveid, String fee,
			String ratio, String expected_loan_time, int creditorid) throws BusinessException {
		String mapMethodName = "updateT0LoanLiveInfoFeeAndRatioByLiveID";
		try {
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("liveid", liveid);
			paramMap.put("fee", fee);
			paramMap.put("ratio", ratio);
			paramMap.put("expected_loan_time", expected_loan_time);
			paramMap.put("loanstatus", IConstDef.LOANLIVETYPE_SETRATE);
			paramMap.put("creditorid", creditorid);
			
			return getSqlSession().update(getSqlName(mapMethodName), paramMap);
		} catch (Exception e) {
			throw new DaoException(String.format("更新T+0贷款申请记录的设定手续费和利率出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}

	@Override
	public int updateT0LoanLiveInfoLimitByLiveID(int liveid, int joinuserid,
			String rm_daily_threshold, String rm_single_threshold, 
			String qryprintscreen, String contractfilepath, String accchangeapplyfilepath) throws BusinessException {
		String mapMethodName = "updateT0LoanLiveInfoLimitByLiveID";
		try {
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("liveid", liveid);
			paramMap.put("joinuserid", joinuserid);
			paramMap.put("rm_daily_threshold", rm_daily_threshold);
			paramMap.put("rm_single_threshold", rm_single_threshold);
			paramMap.put("qryprintscreen", qryprintscreen);
			paramMap.put("contractfilepath", contractfilepath);
			paramMap.put("accchangeapplyfilepath", accchangeapplyfilepath);
			paramMap.put("loanstatus", IConstDef.LOANLIVETYPE_CREDIT);
			
			return getSqlSession().update(getSqlName(mapMethodName), paramMap);
		} catch (Exception e) {
			throw new DaoException(String.format("更新授信额度及保理合同、账户变更申请表文件路径及状态为3出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
		
	}

	@Override
	public String[] queryT0LoanLiveContractFileByJoinuserid(int joinuserid)
			throws BusinessException {
		String mapMethodName = "queryT0LoanLiveContractFileByJoinuserid";
		try {
			List<Object> list = getSqlSession().selectList(getSqlName(mapMethodName), joinuserid);
			if (list != null && list.size() >0) {
				TbT0LoanLiveApplication livevo = (TbT0LoanLiveApplication) list.get(0);
				if (livevo != null) {
					return new String[] {
							livevo.getContractfilepath(), livevo.getAccchangeapplyfilepath()
					};
				}
			}
		} catch (Exception e) {
			throw new DaoException(String.format("查询指定九盈用户申请T+0贷款记录状态为3的信息，获取生成的保理合同和账户变更申请表出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
		return null;
	}

	@Override
	public int uploadT0LoanUserCommitFileByLiveID(int liveid,
			String usercommitapplyfilepath) throws BusinessException {
		String mapMethodName = "uploadT0LoanUserCommitFileByLiveID";
		try {
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("liveid", liveid);
			paramMap.put("usercommitapplyfilepath", usercommitapplyfilepath);
			paramMap.put("loanstatus", IConstDef.LOANLIVETYPE_CONTRACTSIGNING);
			
			return getSqlSession().update(getSqlName(mapMethodName), paramMap);
		} catch (Exception e) {
			throw new DaoException(String.format("更新上传用户盖章后的账户变更申请表及申请状态为4出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
		
	}

	@Override
	public int updateT0LoanLiveInfoUserCommitPass(int liveid)
			throws BusinessException {
		String mapMethodName = "updateT0LoanLiveInfoByLiveIDsAndStatus";
		try {
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("liveids", new int[] { liveid });
			paramMap.put("loanstatus", IConstDef.LOANLIVETYPE_USERCOMMITPASS);
			
			return getSqlSession().update(getSqlName(mapMethodName), paramMap);
		} catch (Exception e) {
			throw new DaoException(String.format("用户提交的账户变更申请表审核通过，更新状态为5出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}

	@Override
	public int uploadT0LoanAccountChangeFileByLiveID(int liveid, String materials) throws BusinessException {
		String mapMethodName = "uploadT0LoanAccountChangeFileByLiveID";
		try {
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("liveid", liveid);
			paramMap.put("materials", materials);
			paramMap.put("loanstatus", IConstDef.LOANLIVETYPE_ACCCHANGEPASS);
			
			return getSqlSession().update(getSqlName(mapMethodName), paramMap);
		} catch (Exception e) {
			throw new DaoException(String.format("更新上传用户盖章后的账户变更申请表及申请状态为4出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
		
	}

	@Override
	public int updateT0LoanLiveInfoUserCommitNoPassByLiveID(int liveid,
			String memo) throws BusinessException {
		String mapMethodName = "updateT0LoanLiveInfoMemoAndLoanStatusByLiveID";
		try {
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("liveid", liveid);
			paramMap.put("loanstatus", IConstDef.LOANLIVETYPE_USERCOMMITNOTPASS);
			paramMap.put("memo", memo);
			
			return getSqlSession().update(getSqlName(mapMethodName), paramMap);
		} catch (Exception e) {
			throw new DaoException(String.format("审核用户提交的账户变更申请表不通过，提交不通过原因并更新状态为6出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}
	
	@Override
	public int updateTxLoanLiveInfoApplyNoPassByLiveID(String operatetype, int liveid,
			String memo) throws BusinessException {
		String mapMethodName = "updateTxLoanLiveInfoMemoAndLoanStatusByLiveID";
		try {
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("liveid", liveid);
			paramMap.put("loanstatus", IConstDef.TX_LOANLIVETYPE_REFUSE);
			paramMap.put("memo", memo);
			paramMap.put("operatetype", operatetype);
			
			return getSqlSession().update(getSqlName(mapMethodName), paramMap);
		} catch (Exception e) {
			throw new DaoException(String.format("审核用户" + operatetype + "业务申请不通过，提交不通过原因并更新状态为-1出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}

	@Override
	public List<TbT0LoanTodoTaskVO> queryAdminUserTodoTaskList()
			throws BusinessException {
		String mapMethodName = "queryAdminUserTodoTaskList";
		try {
			return getSqlSession().selectList(getSqlName(mapMethodName));
		} catch (Exception e) {
			throw new DaoException(String.format("查询后台管理员代办任务列表出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}

	@Override
	public List<TbT0LoanTodoTaskVO> queryOperatorTodoTaskList()
			throws BusinessException {
		String mapMethodName = "queryOperatorTodoTaskList";
		try {
			return getSqlSession().selectList(getSqlName(mapMethodName));
		} catch (Exception e) {
			throw new DaoException(String.format("查询保理员代办任务列表出错！语句：%s",
					getSqlName(mapMethodName)), e);
		}
	}
}
